package com.ly.heart.service.impl;

import java.util.List;
import java.util.Optional;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.SecurityUtils;
import com.ly.common.utils.erp.ConstUtil;
import com.ly.common.utils.erp.ReturnPage;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.BidEntity;
import com.ly.heart.domain.CustomerEntity;
import com.ly.heart.domain.TenderEntity;
import com.ly.heart.domain.dto.StartProcessDto;
import com.ly.heart.service.*;
import org.springframework.stereotype.Service;
import com.ly.heart.mapper.BuyBidMapper;
import com.ly.heart.domain.BuyBidEntity;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

/**
 * 购买标书记录Service业务层处理
 *
 * @author cxl
 * @date 2024-05-14
 */
@Service
public class BuyBidServiceImpl extends ServiceImpl<BuyBidMapper,BuyBidEntity> implements IBuyBidService
{
    @Resource
    private BuyBidMapper buyBidMapper;
    @Resource
    private IBidService bidService;
    @Resource
    private ICustomerService customerInfoService;
    @Resource
    private ITenderService tenderService;
    @Resource
    private IActModelService actModelService;

    @Override
    public BuyBidEntity selectBuyBidVoById(String id){
        BuyBidEntity buyBidEntity = this.getById(id);
        Optional.ofNullable(buyBidEntity).ifPresent(buyBid -> {
            BidEntity bidEntity = bidService.getById(buyBid.getBidId());
            buyBid.setBid(Optional.ofNullable(bidEntity).orElse(new BidEntity()));
            TenderEntity tender = tenderService.getById(buyBid.getTenderId());
            buyBid.setTender( Optional.ofNullable(tender).orElse(new TenderEntity()));
            CustomerEntity customerEntity = customerInfoService.getById(buyBid.getCustomerId());
            buyBid.setCustomer(Optional.ofNullable(customerEntity).orElse(new CustomerEntity()));
        });
        return buyBidEntity;
    }

    @Override
    public AjaxResult selectBuyBidVoList(BuyBidEntity buyBidEntity) {
        List<BuyBidEntity> buyBidList = buyBidMapper.selectBuyBidEntityList(buyBidEntity);
        if(!CollectionUtils.isEmpty(buyBidList)){
            buyBidList.forEach(buyBid -> {
                BidEntity bidEntity = bidService.getById(buyBid.getBidId());
                buyBid.setBid(Optional.ofNullable(bidEntity).orElse(new BidEntity()));
                TenderEntity tender = tenderService.getById(buyBid.getTenderId());
                buyBid.setTender( Optional.ofNullable(tender).orElse(new TenderEntity()));
                CustomerEntity customerEntity = customerInfoService.getById(buyBid.getCustomerId());
                buyBid.setCustomer(Optional.ofNullable(customerEntity).orElse(new CustomerEntity()));
            });
        }
        Page<BuyBidEntity> page = new Page<>(buyBidEntity.getPage(),buyBidEntity.getLimit());
        return AjaxResult.success(ReturnPage.getReturnPage(page,buyBidList));
    }

    /**
     * 查询购买标书记录
     *
     * @param id 购买标书记录主键
     * @return 购买标书记录
     */
    @Override
    public BuyBidEntity selectBuyBidEntityById(String id)
    {
        return buyBidMapper.selectBuyBidEntityById(id);
    }

    /**
     * 查询购买标书记录列表
     *
     * @param buyBidEntity 购买标书记录
     * @return 购买标书记录
     */
    @Override
    public List<BuyBidEntity> selectBuyBidEntityList(BuyBidEntity buyBidEntity)
    {
        return buyBidMapper.selectBuyBidEntityList(buyBidEntity);
    }

    /**
     * 新增购买标书记录
     *
     * @param buyBidEntity 购买标书记录
     * @return 结果
     */
    @Override
    public int insertBuyBidEntity(BuyBidEntity buyBidEntity)
    {
        String buyBidId = SnowflakeIdWorker.getInstance().nextStringId();
        buyBidEntity.setId(buyBidId);
        buyBidEntity.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        buyBidEntity.setCreateTime(DateUtils.getNowDate());
        StartProcessDto startDto = new StartProcessDto().setBusinessId(buyBidId).setBusinessType(buyBidEntity.getBusinessType()).setModelId(buyBidEntity.getModelId());
        startDto.setBusinessName("购买标书_".concat(buyBidId));
        String instanceId = actModelService.startProcess(startDto);
        buyBidEntity.setInstanceId(instanceId);
        buyBidEntity.setState(ConstUtil.STATE_IN_PROCESS);
        return buyBidMapper.insertBuyBidEntity(buyBidEntity);
    }

    /**
     * 修改购买标书记录
     *
     * @param buyBidEntity 购买标书记录
     * @return 结果
     */
    @Override
    public int updateBuyBidEntity(BuyBidEntity buyBidEntity)
    {
        buyBidEntity.setChangeTime(DateUtils.getNowDate());
        return buyBidMapper.updateBuyBidEntity(buyBidEntity);
    }

    /**
     * 批量删除购买标书记录
     *
     * @param ids 需要删除的购买标书记录主键
     * @return 结果
     */
    @Override
    public int deleteBuyBidEntityByIds(String[] ids)
    {
        return buyBidMapper.deleteBuyBidEntityByIds(ids);
    }

    /**
     * 删除购买标书记录信息
     *
     * @param id 购买标书记录主键
     * @return 结果
     */
    @Override
    public int deleteBuyBidEntityById(String id)
    {
        return buyBidMapper.deleteBuyBidEntityById(id);
    }
}
